package com.fenghuajueli.module_user.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.SizeUtils;
import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;
import com.fenghuajueli.module_user.adapter.AvatarAdapter;
import com.fenghuajueli.module_user.adapter.decoration.CustomItemDecoration;
import com.fenghuajueli.module_user.databinding.DialogPickAvatarBinding;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;

public class PickAvatarDialog extends BottomSheetDialog {

    private DialogPickAvatarBinding binding;
    private OnBaseClick<String> picklistener;

    public PickAvatarDialog(@NonNull Context context) {
        super(context, R.style.StyleBaseDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogPickAvatarBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ArrayList<String> imgUrlList = new ArrayList<>();
        for (int i = 1; i < 101; i++) {
            imgUrlList.add("https://tx-ijj-static.ijianji.cn/Ijianji/default/avatar/Upstream" + i + ".png");
        }

        binding.rclAvatar.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        binding.rclAvatar.addItemDecoration(new CustomItemDecoration(getContext(), SizeUtils.dp2px(15), SizeUtils.dp2px(18)));
        binding.rclAvatar.setAdapter(new AvatarAdapter(imgUrlList, new OnBaseClick<String>() {
            @Override
            public void onClick(String s) {
                if (null != picklistener) {
                    picklistener.onClick(s);
                }
                dismiss();
            }
        }));
    }

    @Override
    protected void onStart() {
        super.onStart();
        try {
            WindowManager.LayoutParams attributes = getWindow().getAttributes();
            attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(attributes);
        } catch (Exception e) {
            e.printStackTrace();
        }

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                binding = null;
            }
        });
    }

    public void setPicklistener(OnBaseClick<String> picklistener) {
        this.picklistener = picklistener;
    }
}
